package com.cskaoyan.controller;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @author stone
 * @date 2022/04/29 16:39
 */
@RestController
@RequestMapping("user")
public class UserController {

    //localhost:8080/user/register1?username=songge&password=ligenli
    @RequestMapping("register1")
    public String register1(String username,String password) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return "ok";
    }

    //localhost:8080/user/register2?username=songge&password=ligenli&age=30&married=false
    @RequestMapping("register2")
    //public String register2(String username,String password,String age,String married) {
    //public String register2(String username, String password, Integer age, boolean married) {
    public String register2(String username, String password, int age, boolean married) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);
        System.out.println("married = " + married);
        return "ok";
    }

    //以Date来接收birthday如果想要接收，需要SpringMVC提供String → Date的Converter
    //提供的默认的日期格式是：yyyy/MM/dd
    //localhost:8080/user/register3?username=songge&password=ligenli&age=30&married=false
    //&birthday=2022/04/29
    @RequestMapping("register3")
    public String register3(String username, String password, int age, boolean married, Date birthday) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);
        System.out.println("married = " + married);
        System.out.println("birthday = " + birthday);

        //String date = "2022-04-29";
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Date parse = simpleDateFormat.parse(date);
        return "ok";
    }

    //构造的请求和上面的日期格式不一样：yyyy-MM-dd
    //localhost:8080/user/register4?username=songge&password=ligenli&age=30&married=false
    //&birthday=2022-04-29
    @RequestMapping("register4")
    public String register4(String username, String password, int age, boolean married,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);
        System.out.println("married = " + married);
        System.out.println("birthday = " + birthday);

        //String date = "2022-04-29";
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Date parse = simpleDateFormat.parse(date);
        return "ok";
    }

    //localhost:8080/user/register5?username=songge&password=ligenli&age=30&married=false
    //&birthday=2022-04-29
    //&hobbies=sing&hobbies=dance&hobbies=rap
    //&ids=1&ids=2&ids=3
    @RequestMapping("register5")
    public String register5(String username, String password, int age, boolean married,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday,String[] hobbies,Integer[] ids) {
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);
        System.out.println("married = " + married);
        System.out.println("birthday = " + birthday);
        System.out.println("hobbies = " + Arrays.asList(hobbies));
        System.out.println("ids = " + Arrays.asList(ids));

        return "ok";
    }
}
